package janelas;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.text.MaskFormatter;
import outros.Documento;
import outros.GeradorRelatorio;
import outros.Util;
import persistencia.DAOFornecedor;
import persistencia.DAOGarantia;
import persistencia.DAOGenerico;
import persistencia.DAOPatrimonio;
import persistencia.DAOSetor;
import principal.Principal;
import transporte.TOFornecedor;
import transporte.TOGarantia;
import transporte.TOGenerico;
import transporte.TOPatrimonio;

public class Garantia extends javax.swing.JInternalFrame implements JanelaPai {
    private DAOFornecedor daoFornecedor = new DAOFornecedor();
    private ArrayList<TOFornecedor> toFornecedor = new ArrayList();
    private DAOGenerico daoOrgao = new DAOGenerico();
    private ArrayList<TOGenerico> toOrgao = new ArrayList();
    private GeradorRelatorio gr = new GeradorRelatorio();
    private TOPatrimonio[] escolhidos = new TOPatrimonio[]{
                                        new TOPatrimonio(),new TOPatrimonio(),
                                        new TOPatrimonio(),new TOPatrimonio(),
                                        new TOPatrimonio()};
    private TOPatrimonio patrimonio = null;
    private DAOPatrimonio daoPatrimonio = new DAOPatrimonio();
    private DAOGarantia daoGarantia = new DAOGarantia();
    private DAOSetor daoSetor = new DAOSetor();
    private Util util = new Util();
    private MaskFormatter mascaraData;

    public Garantia() {
        for (int i=0;i<escolhidos.length;i+=1) {
            escolhidos[i].setNumero(-1);
        }
        try {
            mascaraData = new MaskFormatter("##/##/####");
        } catch (Exception e) {}
        initComponents();
        toOrgao = daoOrgao.selecionarGenerico("orgao", "nome");
        tabela.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-getWidth())/2, (int)((screenSize.height-getHeight())/3), getWidth(), getHeight());
        preencherComboBox();
    }

    public void reiniciar() {        
        camOrgao.removeAllItems();
        preencherComboBox();
        limparCampos();
    }

    private void limparCampos() {
        for (int i=0;i<escolhidos.length;i+=1) {
            tabela.setValueAt("", i, 0);
            tabela.setValueAt("", i, 1);
            tabela.setValueAt("", i, 2);
            escolhidos[i].setNumero(-1);
        }
        camFornecedor.setSelectedIndex(0);
        camOrgao.setSelectedIndex(0);
        //camBuscaArea.setText("");
        //camBuscaGrupo.setText("");
        //camBuscaSubgrupo.setText("");
        //camBuscaNumero.setText("");        
        //camData.setText("");
        //camTipo.setSelectedIndex(0);
        camPatrimonio.setText("");
        camBuscaArea.requestFocus();
    }

    private void preencherComboBox() {
        toFornecedor = daoFornecedor.selecionar("");
        camFornecedor.removeAllItems();
        for (int i=0;i<toFornecedor.size();i+=1) {
            camFornecedor.addItem(toFornecedor.get(i).getNome());
        }
        for (int i=0;i<toOrgao.size();i+=1) {
            camOrgao.addItem(toOrgao.get(i).getNome());
        }
    }

    private int[] verificarCampos() {
        int[] valores = new int[5];
        if (camBuscaArea.getText().length()>0) {
            valores[0] = Integer.parseInt(camBuscaArea.getText());
        } else {
            valores[0] = -1;
        }
        if (camBuscaGrupo.getText().length()>0) {
            valores[1] = Integer.parseInt(camBuscaGrupo.getText());
        } else {
            valores[1] = -1;
        }
        if (camBuscaSubgrupo.getText().length()>0) {
            valores[2] = Integer.parseInt(camBuscaSubgrupo.getText());
        } else {
            valores[2] = -1;
        }
        if (camBuscaNumero.getText().length()>0) {
            valores[3] = Integer.parseInt(camBuscaNumero.getText());
        } else {
            valores[3] = -1;
        }
        valores[4] = toOrgao.get(camOrgao.getSelectedIndex()).getNumero();
        return valores;
    }

    private void buscarPatrimonio() {
        int[] valores = verificarCampos();
        for (int i=0;i<valores.length;i+=1) {
            if (valores[i]==-1) {
                JOptionPane.showMessageDialog(null, "Preencha todos os campos.");
                camBuscaArea.requestFocus();
                return;
            }
        }
        patrimonio = daoPatrimonio.selecionarRegistro(valores[0], valores[1], valores[2], valores[3], valores[4], -1);
        if (patrimonio!=null) {
            if (camTipo.getSelectedIndex()==0) {
                if (patrimonio.getGarantia()==0) {
                    JOptionPane.showMessageDialog(null, "O patrimônio não está na garantia.", "AVISO", JOptionPane.WARNING_MESSAGE);
                    patrimonio = null;
                    camPatrimonio.setText("");
                    camBuscaArea.requestFocus();
                    return;
                }
            } else {
                if (patrimonio.getGarantia()==1 || patrimonio.getEmprestimo()==1) {
                    JOptionPane.showMessageDialog(null, "O patrimônio já está na garantia ou emprestado.", "AVISO", JOptionPane.WARNING_MESSAGE);
                    patrimonio = null;
                    camPatrimonio.setText("");
                    camBuscaArea.requestFocus();
                    return;
                }
            }
            camPatrimonio.setText(patrimonio.getNome());
            botAdicionar.requestFocus();
        } else {
            JOptionPane.showMessageDialog(null, "Não foi possível encontrar nenhum patrimônio.");
            camPatrimonio.setText("");
        }
    }

    private String[] getParametros() {
        String[] parametros = new String[6];
        TOFornecedor toFornecedorTemp = daoFornecedor.selecionarRegistro(toFornecedor.get(camFornecedor.getSelectedIndex()).getNumero());
        parametros[0] = toFornecedorTemp.getNome();
        parametros[1] = toFornecedorTemp.getEndereco();
        parametros[2] = toFornecedorTemp.getSetor();
        JFormattedTextField campo = null;
        try {
            MaskFormatter mascara = new MaskFormatter("(##)####-####");
            campo = new JFormattedTextField();
            mascara.install(campo);
            campo.setText(toFornecedorTemp.getTelefone());
            parametros[3] = campo.getText();
        } catch (ParseException ex) {
            parametros[3] = null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        parametros[4] = sdf.format(util.getData());
        parametros[5] = (String)camTipo.getSelectedItem();
        return parametros;
    }

    private String[] getLocais() {
        String[] locais = new String[5];
        for (int i=0;i<5;i+=1) {
            String temp = (String)tabela.getValueAt(i, 1);
            try {
                temp = temp.substring(0, 18).concat(".");
            } catch (Exception e) {}
            locais[i] = temp;
        }
        return locais;
    }

    private String[] getObs() {
        String[] obs = new String[5];
        for (int i=0;i<5;i+=1) {
            obs[i] = (String)tabela.getValueAt(i, 2);
        }
        return obs;
    }

    private boolean jaEstaNaTabela(int[] dados) {
        for (int i=0;i<escolhidos.length;i+=1) {
            if (escolhidos[i].getNumero()!=-1) {
                if (escolhidos[i].getOrgao()==dados[0] && escolhidos[i].getArea()==dados[1] &&
                    escolhidos[i].getGrupo()==dados[2] && escolhidos[i].getSubgrupo()==dados[3] &&
                    escolhidos[i].getNumero()==dados[4]) {
                    return true;
                }
            }
        }
        return false;
    }

    private void adicionarPatrimonio(String pat) {
        if (!jaEstaNaTabela(new int[]{patrimonio.getOrgao(), patrimonio.getArea(),
                                     patrimonio.getGrupo(), patrimonio.getSubgrupo(),
                                     patrimonio.getNumero()})) {
            for (int i=0;i<escolhidos.length;i+=1) {
                if (escolhidos[i].getNumero()==-1) {
                    escolhidos[i].setOrgao(patrimonio.getOrgao());
                    escolhidos[i].setArea(patrimonio.getArea());
                    escolhidos[i].setGrupo(patrimonio.getGrupo());
                    escolhidos[i].setSubgrupo(patrimonio.getSubgrupo());
                    escolhidos[i].setNumero(patrimonio.getNumero());
                    escolhidos[i].setNome(pat);
                    tabela.setValueAt(pat, i, 0);
                    String local = daoSetor.getAtributo(escolhidos[i].getNumero(), "nome");
                    tabela.setValueAt(local, i, 1);
                    break;
                } else if (i==escolhidos.length-1) {
                    JOptionPane.showMessageDialog(null, "Todos os espaços para patrimônio estão cheios.");
                }
            }
        } else {
            JOptionPane.showMessageDialog(null, "O patrimônio já está na lista.", "AVISO", JOptionPane.WARNING_MESSAGE);
        }
    }

    private void removerPatrimonio(int linha) {
        tabela.setValueAt("", linha, 0);
        tabela.setValueAt("", linha, 1);
        tabela.setValueAt("", linha, 2);
        escolhidos[linha].setNumero(-1);
    }

    private ArrayList<TOGarantia> getGarantias() {
        ArrayList<TOGarantia> gar = new ArrayList();
        TOGarantia g = null;
        for (int i=0;i<escolhidos.length;i+=1) {
            if (escolhidos[i].getNumero()!=-1) {
                g = new TOGarantia();
                g.setPatrimonio(escolhidos[i]);
                g.setData(util.getData());
                g.setHora(util.getHora());
                g.setUsuario(Principal.getUsuarioLogado().getNumero());
                g.setFornecedor(toFornecedor.get(camFornecedor.getSelectedIndex()).getNumero());
                g.setTipo(camTipo.getSelectedIndex());
                gar.add(g);
            }
        }
        return gar;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel30 = new javax.swing.JLabel();
        camFornecedor = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabela = new javax.swing.JTable();
        botLimpar = new javax.swing.JButton();
        botBuscar2 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel28 = new javax.swing.JLabel();
        camBuscaArea = new javax.swing.JTextField();
        jLabel25 = new javax.swing.JLabel();
        camBuscaGrupo = new javax.swing.JTextField();
        jLabel26 = new javax.swing.JLabel();
        camBuscaSubgrupo = new javax.swing.JTextField();
        jLabel27 = new javax.swing.JLabel();
        camBuscaNumero = new javax.swing.JTextField();
        jLabel29 = new javax.swing.JLabel();
        camOrgao = new javax.swing.JComboBox();
        botBuscar = new javax.swing.JButton();
        camPatrimonio = new javax.swing.JTextField();
        botAdicionar = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        camTipo = new javax.swing.JComboBox();

        setClosable(true);
        setTitle("Movimentar Bem - Garantia/Manutenção");

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Movimentação", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Segoe UI", 0, 13))); // NOI18N

        jLabel30.setFont(new java.awt.Font("Tahoma", 0, 13));
        jLabel30.setText("Fornecedor");

        camFornecedor.setFont(new java.awt.Font("Segoe UI", 0, 13));

        tabela.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Bens", "Local", "Observação"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabela.setColumnSelectionAllowed(true);
        tabela.getTableHeader().setReorderingAllowed(false);
        tabela.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tabelaKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(tabela);
        tabela.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tabela.getColumnModel().getColumn(0).setResizable(false);
        tabela.getColumnModel().getColumn(0).setPreferredWidth(225);
        tabela.getColumnModel().getColumn(1).setResizable(false);
        tabela.getColumnModel().getColumn(1).setPreferredWidth(50);
        tabela.getColumnModel().getColumn(2).setResizable(false);
        tabela.getColumnModel().getColumn(2).setPreferredWidth(50);

        botLimpar.setFont(new java.awt.Font("Segoe UI", 0, 13));
        botLimpar.setText("Limpar Campos");
        botLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botLimparActionPerformed(evt);
            }
        });

        botBuscar2.setFont(new java.awt.Font("Segoe UI", 0, 13));
        botBuscar2.setText("OK");
        botBuscar2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botBuscar2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 609, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel30)
                    .addComponent(camFornecedor, 0, 407, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botLimpar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botBuscar2))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8)
                .addComponent(jLabel30)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(camFornecedor, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botBuscar2)
                    .addComponent(botLimpar))
                .addContainerGap())
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Buscar Patrimônio", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Segoe UI", 0, 13))); // NOI18N

        jLabel28.setFont(new java.awt.Font("Tahoma", 0, 13));
        jLabel28.setText("Área");

        camBuscaGrupo.setDocument(new Documento(true, 5));
        camBuscaArea.setFont(new java.awt.Font("Segoe UI", 0, 13));
        camBuscaArea.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                camBuscaAreaActionPerformed(evt);
            }
        });
        camBuscaArea.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                camBuscaAreaFocusGained(evt);
            }
        });

        jLabel25.setFont(new java.awt.Font("Tahoma", 0, 13));
        jLabel25.setText("Grupo");

        camBuscaGrupo.setDocument(new Documento(true, 5));
        camBuscaGrupo.setFont(new java.awt.Font("Segoe UI", 0, 13));
        camBuscaGrupo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                camBuscaGrupoActionPerformed(evt);
            }
        });
        camBuscaGrupo.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                camBuscaGrupoFocusGained(evt);
            }
        });

        jLabel26.setFont(new java.awt.Font("Tahoma", 0, 13));
        jLabel26.setText("Subgrupo");

        camBuscaSubgrupo.setDocument(new Documento(true, 5));
        camBuscaSubgrupo.setFont(new java.awt.Font("Segoe UI", 0, 13));
        camBuscaSubgrupo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                camBuscaSubgrupoActionPerformed(evt);
            }
        });
        camBuscaSubgrupo.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                camBuscaSubgrupoFocusGained(evt);
            }
        });

        jLabel27.setFont(new java.awt.Font("Tahoma", 0, 13));
        jLabel27.setText("Número");

        camBuscaNumero.setDocument(new Documento(true, 5));
        camBuscaNumero.setFont(new java.awt.Font("Segoe UI", 0, 13));
        camBuscaNumero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                camBuscaNumeroActionPerformed(evt);
            }
        });
        camBuscaNumero.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                camBuscaNumeroFocusGained(evt);
            }
        });

        jLabel29.setFont(new java.awt.Font("Tahoma", 0, 13));
        jLabel29.setText("Órgão");

        camOrgao.setFont(new java.awt.Font("Segoe UI", 0, 13));

        botBuscar.setFont(new java.awt.Font("Segoe UI", 0, 13));
        botBuscar.setText("Buscar");
        botBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botBuscarActionPerformed(evt);
            }
        });

        camBuscaGrupo.setDocument(new Documento(true, 5));
        camPatrimonio.setEditable(false);
        camPatrimonio.setFont(new java.awt.Font("Segoe UI", 0, 13));
        camPatrimonio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                camPatrimonioActionPerformed(evt);
            }
        });

        botAdicionar.setFont(new java.awt.Font("Segoe UI", 0, 13));
        botAdicionar.setText("Adicionar");
        botAdicionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botAdicionarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(camOrgao, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel29))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(camBuscaArea, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel28))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel25)
                            .addComponent(camBuscaGrupo, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel26)
                            .addComponent(camBuscaSubgrupo, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(camBuscaNumero, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(botBuscar))
                            .addComponent(jLabel27)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(camPatrimonio)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botAdicionar)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel29)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel27)
                            .addComponent(jLabel26))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel28)
                            .addComponent(jLabel25))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(camBuscaSubgrupo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(camBuscaNumero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(camBuscaGrupo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(camBuscaArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(camOrgao, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botBuscar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(camPatrimonio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botAdicionar))
                .addContainerGap())
        );

        jLabel6.setFont(new java.awt.Font("Segoe UI", 0, 13));
        jLabel6.setText("Tipo");

        camTipo.setFont(new java.awt.Font("Segoe UI", 0, 13)); // NOI18N
        camTipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Entrada na Agecom", "Saída da Agecom" }));
        camTipo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                camTipoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(camTipo, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(camTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botBuscar2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botBuscar2ActionPerformed
        ArrayList<TOGarantia> g = getGarantias();
        if (g.size()>0) {
            if (camTipo.getSelectedIndex()==1) {
                if (JOptionPane.showConfirmDialog(null, "Confirma a ida para garantia?")==0) {
                    daoGarantia.criarRegistro(g);
                    gr.geraTermoGarantia(getParametros(), escolhidos, getLocais(), getObs());
                    limparCampos();
                }
            } else {
                if (JOptionPane.showConfirmDialog(null, "Confirma o retorno da garantia?")==0) {
                    daoGarantia.criarRegistro(g);
                    limparCampos();
                }
            }
        } else {
            JOptionPane.showMessageDialog(null, "Selecione pelo menos um item para ser encaminhado à garantia.");
        }
}//GEN-LAST:event_botBuscar2ActionPerformed

    private void tabelaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tabelaKeyReleased
        if (evt.getKeyCode()==KeyEvent.VK_DELETE) {
            removerPatrimonio(tabela.getSelectedRow());
        }
}//GEN-LAST:event_tabelaKeyReleased

    private void camBuscaNumeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_camBuscaNumeroActionPerformed
        buscarPatrimonio();
}//GEN-LAST:event_camBuscaNumeroActionPerformed

    private void botBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botBuscarActionPerformed
        buscarPatrimonio();
}//GEN-LAST:event_botBuscarActionPerformed

    private void camPatrimonioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_camPatrimonioActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_camPatrimonioActionPerformed

    private void botAdicionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botAdicionarActionPerformed
        if (camPatrimonio.getText().length()>0) {
            adicionarPatrimonio(camPatrimonio.getText());
            camBuscaArea.requestFocus();
        } else {
            JOptionPane.showMessageDialog(null, "Nenhum patrimônio selecionado.");
        }
}//GEN-LAST:event_botAdicionarActionPerformed

    private void botLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botLimparActionPerformed
        limparCampos();
}//GEN-LAST:event_botLimparActionPerformed

    private void camTipoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_camTipoActionPerformed
        limparCampos();
    }//GEN-LAST:event_camTipoActionPerformed

    private void camBuscaAreaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_camBuscaAreaActionPerformed
        camBuscaGrupo.requestFocus();
    }//GEN-LAST:event_camBuscaAreaActionPerformed

    private void camBuscaGrupoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_camBuscaGrupoActionPerformed
        camBuscaSubgrupo.requestFocus();
    }//GEN-LAST:event_camBuscaGrupoActionPerformed

    private void camBuscaSubgrupoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_camBuscaSubgrupoActionPerformed
        camBuscaNumero.requestFocus();
    }//GEN-LAST:event_camBuscaSubgrupoActionPerformed

    private void camBuscaAreaFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_camBuscaAreaFocusGained
        camBuscaArea.selectAll();
    }//GEN-LAST:event_camBuscaAreaFocusGained

    private void camBuscaGrupoFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_camBuscaGrupoFocusGained
        camBuscaGrupo.selectAll();
    }//GEN-LAST:event_camBuscaGrupoFocusGained

    private void camBuscaSubgrupoFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_camBuscaSubgrupoFocusGained
        camBuscaSubgrupo.selectAll();
    }//GEN-LAST:event_camBuscaSubgrupoFocusGained

    private void camBuscaNumeroFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_camBuscaNumeroFocusGained
        camBuscaNumero.selectAll();
    }//GEN-LAST:event_camBuscaNumeroFocusGained


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botAdicionar;
    private javax.swing.JButton botBuscar;
    private javax.swing.JButton botBuscar2;
    private javax.swing.JButton botLimpar;
    private javax.swing.JTextField camBuscaArea;
    private javax.swing.JTextField camBuscaGrupo;
    private javax.swing.JTextField camBuscaNumero;
    private javax.swing.JTextField camBuscaSubgrupo;
    private javax.swing.JComboBox camFornecedor;
    private javax.swing.JComboBox camOrgao;
    private javax.swing.JTextField camPatrimonio;
    private javax.swing.JComboBox camTipo;
    private javax.swing.JLabel jLabel25;
    private javax.swing.JLabel jLabel26;
    private javax.swing.JLabel jLabel27;
    private javax.swing.JLabel jLabel28;
    private javax.swing.JLabel jLabel29;
    private javax.swing.JLabel jLabel30;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabela;
    // End of variables declaration//GEN-END:variables

}
